package com.itheima.dao;

import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import org.apache.ibatis.annotations.*;
import org.springframework.cglib.transform.impl.InterceptFieldFilter;

import java.util.List;
import java.util.Map;

public interface SetMealDao {

    /**
     * 新增套餐
     * @param setmeal
     */
    @Insert("insert into t_setmeal " +
            "values " +
            "(null,#{name},#{code},#{helpCode},#{sex},#{age},#{price},#{remark},#{attention},#{img})")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    public void save(Setmeal setmeal);

    /**
     * 新增套餐结束后 新增中间表关联关系
     * @param map
     */
    @Insert("insert into t_setmeal_checkgroup values (#{setmeal_id},#{checkgroup_id})")
    public void saveInnerTable(Map map);

    /**
     * 分页查询 or 全查询 （动态sql）
     * @param value
     * @return
     */
    @Select("<script> " +
            "select * from t_setmeal " +
            "<where>" +
            "<if test='value!=null'>" +
            "code=#{value} or helpCode=#{value} or name like concat('%',#{value},'%')" +
            "</if>" +
            "</where>" +
            "</script>")
    public List<Setmeal> findAll(String value);

    /**
     *回显使用
     * @param id
     * @return
     */
    @Select("select * from t_setmeal_checkgroup where setmeal_id=#{id}")
    public List<Integer> findCheckGroupSort(@Param("id") Integer id);

    /**
     * 要更新 先删除中间表关系
     * @param setmealId
     */
    @Delete("delete from t_setmeal_checkgroup where setmeal_id=#{setmealId}")
    public void deleteInner(@Param("setmealId") Integer setmealId);

    /**
     * update t_setmeal 表
     * @param setmeal
     */
    @Update("update t_setmeal set name=#{name},code=#{code},helpCode=#{helpCode},sex=#{sex},age=#{age},price=#{price},remark=#{remark},attention=#{attention},img=#{img} where id=#{id}")
    public void updateSetMeal(Setmeal setmeal);

    /**
     * delete by id
     * @param id
     */
    @Delete("delete from t_setmeal where id=#{id}")
    public void deleteSetMeal(Integer id);

    @Select("select * from t_setmeal where id=#{id}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "code",property = "code"),
            @Result(column = "helpCode",property = "helpCode"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "age",property = "age"),
            @Result(column = "price",property = "price"),
            @Result(column = "remark",property = "remark"),
            @Result(column = "attention",property = "attention"),
            @Result(column = "img",property = "img"),
            @Result(
                    column = "id",
                    property = "checkGroups",
                    javaType = List.class,
                    many = @Many(select = "com.itheima.dao.CheckGroupDao.findByInnerId")
            ),
    })
    public Setmeal findById(@Param("id") Integer id);
}
